//
// Created by zjb on 2020/10/11.
//

#ifndef MEDIA_MIXER_MEDIAMIXER_H
#define MEDIA_MIXER_MEDIAMIXER_H
#include<stdint.h>
#include<string>
#include<memory>
#include<functional>
#ifdef MATHLIBRARY_EXPORTS
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API __declspec(dllimport)
#endif

namespace liyuvideo {

    class MyMediaBuf{
    public:
        MyMediaBuf();
        MyMediaBuf(char*p,int l,uint32_t ts = 0,bool bEnd = false){
            m_buf = p;
            m_len = l;
            m_ts = ts;
            m_bEnd = bEnd;
        };
        ~MyMediaBuf(){
            if(m_buf!=nullptr){
                //printf("free media buf\n");
                delete[] m_buf;
                m_len = 0;
            }
        }
        char* m_buf = nullptr;
        int m_len = 0;
        uint32_t m_ts;
        bool m_bEnd = false;
    };
    class VideoSource{
    public:
        /**
         *
         * @param pVideoFrame
         * @param len
         * @param ts   时间，单位毫秒
         * @param bEnd 是否是最后一帧，编码完后结束
         */
        virtual void feedVideoFrame(std::shared_ptr<MyMediaBuf>& pVideoFrame) = 0;
    };

    class AudioSource{
    public:
        /**
         *
         * @param pAudioFrame
         * @param len        没有音频帧时 len为0
         * @param ts
         */
        virtual void feedAudioFrame(std::shared_ptr<MyMediaBuf>& pAudioFrame) = 0;
    };

    typedef std::function<void()> CompleteEvent;
    class ExportConfig {
    public:
        ExportConfig(){
            videoWidth = videoHeight = 0;
            fps = 25;videoBitrate = 1000;
            audioChannels = 2;
            audioSamplerate = 44100;
            audioBitrate = 128;
            m_pVideoSource = nullptr;
            m_pAudioSourceList[0] = nullptr;
            m_pAudioSourceList[1] = nullptr;
            m_pAudioSourceList[2] = nullptr;
            m_pCompleteEvent = nullptr;
            format="mp4";
        }
        int videoWidth;
        int videoHeight;
        int fps;       //帧率
        int videoBitrate; //视频码率(kbps)
        int audioChannels; //声道数 1 or 2
        int audioSamplerate; //音频采集率 44100 480000
        int audioBitrate;    //音频码率(kbps) 比如 128 256
        VideoSource*m_pVideoSource;
        AudioSource* m_pAudioSourceList[3];
        std::string m_videoFile;
        CompleteEvent m_pCompleteEvent;
        std::string format; //mp4,flv,推流到rtmp服务器，format要指定为flv
    };

    class MediaExport {
    public:
        virtual int initModule(std::shared_ptr<ExportConfig> config) = 0;
        virtual void releaseModule() = 0;
    };

    EXPORT_API MediaExport*getMediaExport();
};


#endif //MEDIA_MIXER_MEDIAMIXER_H
